<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工管理</title>
<!-- 导入jquery核心类库 -->
 <%@include file="../include/jqueryeasyui.jsp"%>
 

<script type="text/javascript">
function initDeptCombobox(){
	var sessionStaff=<%=session.getAttribute("staff")%>; 
	var url;
	if(sessionStaff.sLoginName=="admin"){
	 	 url ='${pageContext.request.contextPath}/page.deptListAll.ashx'
	}else {
		
	 	url = '${pageContext.request.contextPath}/page.everyone.getDeptById.ashx?dId='+sessionStaff.sDeptId;
		
	}
	
 	
	 $('#dept').combobox({
     	url:url,
 		method:'get',
		valueField:'dId',
		textField:'dName',
		panelHeight:'300',
		filter: function(q, row){  
	        var opts = $(this).combobox('options');  
	        return row[opts.textField].indexOf(q) >= 0;//这里改成>=即可在任意地方匹配  
	    }
 
	 });
	
}

	function doAdd(){
		$('#tr_role').show();
		$('#tr_role_warning').hide();
		$('#td_role_tag').html('添加角色权限');
		//alert("增加...");
		//$("input[name='sId']").val('-1');
		$('#addStandardWindow').form('clear');
		$('#addStandardWindow *').trigger('blur');// 离焦
		$('#addStandardWindow').window("open").window('setTitle', '添加员工');
		$("input[name='sId']").val('-1'); 
		$("input[name='userType']").val('1'); 
	}
	
	function getQueryParams(queryParams) {  
		var searchcontent = $('#searchcontent').val();
	     queryParams.sName = searchcontent;  
	     return queryParams;  
	   
	 }  
	
	 //增加查询参数，重新加载表格  
    function reloadgrid() {  
  
        //查询参数直接添加在queryParams中      
        var queryParams = $('#grid').datagrid('options').queryParams;  
        getQueryParams(queryParams);  
        $('#grid').datagrid('options').queryParams = queryParams;  
        $("#grid").datagrid('load');  
    }  
	 
    function doExport(){
    	$.messager.alert('提示', "正在完善中...",'info')
    }
	 function doSearch(){
		
		//获取查询参数
		//查询
		//刷新easyui 
		/* var searchcontent = $('#searchcontent').val();
		//alert(searchcontent)
		if(null!=searchcontent&&searchcontent!=''){
			
			reloadgrid();
		}else{
			$.messager.alert('消息','<br>请输入查询内容 ','info');
		} */			
		reloadgrid();
	}
	
	function doEdit(){
		var rowData = $('#grid').datagrid('getSelected');  
        
        if (rowData == null) {  
            $.messager.alert("提示", "请选择要修改的行！", "info");  
        } else{
	        $("input[name='sId']").val(rowData.sId);
			$("input[name='sName']").val(rowData.sName);
			$("input[name='sLoginName']").val(rowData.sLoginName);
			$("input[name='sEmail']").val(rowData.sEmail);
			$("input[name='sPhone']").val(rowData.sPhone);
			$("input[name='sWeChat']").val(rowData.sWeChat);
			$("input[name='sQQ']").val(rowData.sQQ);
			$("input[name='sSubPhone']").val(rowData.sSubPhone);
			$("input[name='sTitle']").val(rowData.sTitle);
			$("input[name='userType']").val('1');
		
			
			$('#sPwd').val(rowData.sPwd);
 			
			$('#sStatus').combobox('setValue', rowData.sStatus);
			/* $('#userType').combobox('setValue', rowData.userType); */
 			$('#menuIds').combobox('setValue', '');
 	 		$('#dept').combobox('select', rowData.sDeptId);
	 		$('#tr_role').hide();
	 		$('#tr_role_warning').show();
  
			// 弹出窗口
			  
			$("#addStandardWindow").window('open').window('setTitle', '修改员工');
        } 
	}
	
	function showRoleElement(){
		$('#td_role_tag').html('重置权限');
		$('#tr_role').show();

 	}
	

	// 定义列
	var columns = [ [  {
		field : 'sName',
		title : '姓名',
		width : 110,	
		
	},  {
		field : 'sLoginName',
		title : '登录名',
		width : 110,
		
	},  {
		field : 'sDeptId',
		title : '部门',
		width : 110,
		sortable:true,
		
		formatter:formatterDeptId
	},  {
		field : 'sTitle',
		title : '职务',
		width : 110,
		sortable:true,
		
	}, {
		field : 'sEmail',
		title : '邮箱',
		width : 150,
		
	}, {
		field : 'sPhone',
		title : '电话',
		width : 110,
		
	}, {
		field : 'sSubPhone',
		title : '分机号',
		width : 40,
		
	}, {
		field : 'sQQ',
		title : 'QQ号',
		width : 90,
		
	}, {
		field : 'sWeChat',
		title : '微信号',
		width : 90,
		
	},/* {
		field : 'userType',
		title : '用户类型',
		width : 90,
		sortable:true,
		formatter:formatterUserType
		
	}, */ {
		field : 'sstatus',
		title : '是否在职',
		width : 50,
		sortable:true,
		
		formatter:formatterStatus
	}, {
		field : 'sId',
		title : '操作',
		width : 160,
		
		formatter:formaterFuQuan
	} ] ];
	
	function formaterFuQuan(value,row,index){
		if(row.sLoginName!='admin'){
			return "&nbsp;&nbsp;&nbsp;&nbsp;<a onclick=doDel("+index+") href=#>删除</a>";
		}else{
			return '';
		}
		
	}
	
	function formatterUserType(value,row,index){
		if(value==1){
			return "管理员";
		}else{
			return '普通用户';
		}
		
	}
	
 
	
	function doDel(index){
	    $('#grid').datagrid('selectRow',index);// 关键在这里  
	    var row = $('#grid').datagrid('getSelected'); 
	    if(row){ 

		     $.messager.confirm("操作提示", "确认删除 当前记录？", function (data) {  
		            if (data) {  
		            	$.post("${pageContext.request.contextPath}/page.staffdel.ashx?sId="+row.sId,function(data){
			    			
			    			if(data.status == "1"){
			    				// 成功
			    				$.messager.alert('消息','操作成功 ','info');
			    				reloadgrid();
			    				
			    			
			    			}else{
			    					$.messager.alert('消息','<br>操作失败','info');
			    				
			    			}
			    				
			    		}); 
		            }  
		            else {  
		                 
		            }  
		        }); 


		}
	}
	
	function doFuQuan(index){
	    $('#grid').datagrid('selectRow',index);// 关键在这里  
	    var row = $('#grid').datagrid('getSelections'); 
	    if(row.length>1){
	    	$.messager.alert('提示', '您选择了多条数据，请选择一条数据进行操作', 'warning');
	    	return;
	    	
	    }

	    if(row){ 
			location.href="${pageContext.request.contextPath}/page.staffmenus.ashx?sId="+row[0].sId;
	    	
		}
	}
	
	function formatterSex(value,row,index){
		if(row.sSex==1){
			return "男";
		}else if(row.sSex==0){
			return "女";
		}
	}
	
	function formatterDeptId(value,row,index){
		/* if(row.sSex==1){
			return "男";
		}else if(row.sSex==0){
			return "女";
		} */
		
		var dName;
		$.ajax({type:'post',
			url : "${pageContext.request.contextPath}/page.get.ashx",
			data:{dId:value},
			async:false,
			success:function(data){dName=data.dName}
		}); 
		 return dName;                 
        	
	}
	
	function formatterStatus(value,row,index){
		if(row.sStatus==1){
			return "离职";
		}else if(row.sStatus==0){
			return "在职";
		}
	}
	$(function(){
		$('#add').click(doAdd);
 		$('#edit').click(doEdit);
		$('#search').click(doSearch);
		$('#export').click(doExport);
		initDeptCombobox();
 	});
	$(function(){
		// 先将body隐藏，再显示，不会出现页面刷新效果
		$("body").css({visibility:"visible"});
		
		// 收派标准数据表格
		$('#grid').datagrid( {
			iconCls : 'icon-forward',
			fit : true,
			border : false,
			rownumbers : true,
			singleSelect:true,
			striped : true,
			toolbar : '#tb',
			url : "${pageContext.request.contextPath}/page.kuguanList.ashx",
			remoteSort:false,
			idField : 'sId',
			columns : columns,
			pageList: [10,50,100,500],
			pagination:true,
			/* onDblClickRow : doDblClickRow, */
			view:myview,
			emptyMsg:"<font size=5>暂无记录</font>",
			/* singleSelect: false,
			selectOnCheck: true,
			checkOnSelect: true
			 */
		});
		
		//初始化员工窗口
		$('#addStandardWindow').window({
            title: '员工详情',
            width: 800,
            modal: true,
            shadow: true,
            closed: true,
            height: 480,
            resizable:false
            
        });
		
	
		
		//提交表单
		$('#save').click(function(){
			var select = $('#dept').combobox('panel').find('.combobox-item-selected') 
			if(select.length==0){
				$.messager.alert('消息','请在下拉框中选取项目！','info');
				$('#dept').combobox('clear');
				return;
			}
				// 校验
				if($('#sform').form('validate')){
					// 1 、获得form 数据
					var params = $('#sform').serialize();
					// 2、将数据发送给服务器 
					$.post("${pageContext.request.contextPath}/page.saveOrUpdateKuguan.ashx", params , function(data){
						if(data == "success"){
							// 成功
							$.messager.alert('消息','操作成功 ','info');
							// 关闭窗口
							$('#addStandardWindow').window('close');
							// 清除表单数据
							$("input[name='sId']").val('');
							$('#addStandardWindow').form('clear');
							$('#addStandardWindow *').trigger('blur');// 离焦
							
							// 刷新表格数据 
							$('#grid').datagrid('reload');
						}else if(data=="conflict"){
							$.messager.alert('消息','登录名已经被占用！ ','info');
						}else if(data=="phoneconflict"){
							$.messager.alert('消息','手机号已经被占用！ ','info');
						}else if(data=="admin"){
							$.messager.alert('消息','admin无法更改登录名、在职状态、用户角色！ ','info');
						}else{
							$.messager.alert('消息','操作失败 ','info');
						}
							
					});
				}
				
			});	
		
		
	});
	
	
	
	var myview = $.extend({},$.fn.datagrid.defaults.view,{
		onAfterRender:function(target){
			$.fn.datagrid.defaults.view.onAfterRender.call(this,target);
			var opts = $(target).datagrid('options');
			var vc = $(target).datagrid('getPanel').children('div.datagrid-view');
			vc.children('div.datagrid-empty').remove();
			if (!$(target).datagrid('getRows').length){
				var d = $('<div class="datagrid-empty"></div>').html(opts.emptyMsg || 'no records').appendTo(vc);
				d.css({
					position:'absolute',
					left:0,
					top:50,
					width:'100%',
					textAlign:'center'
				});
			}
		}
	});
	$.extend($.fn.validatebox.defaults.rules, {   
	    //验证汉字   
	    CHS: {   
	        validator: function (value) {   
	            return /^[\u0391-\uFFE5]+$/.test(value);   
	        },   
	        message: '只能输入汉字'  
	    },   
	    //移动手机号码验证   
	    mobile: {//value值为文本框中的值   
	        validator: function (value) {   
	            var reg = /^1[3|4|5|7|8|9]\d{9}$/;   
	            return reg.test(value);   
	        },   
	        message: '输入手机号码格式不准确.'  
	    },   
	    //国内邮编验证   
	    zipcode: {   
	        validator: function (value) {   
	            var reg = /^[1-9]\d{5}$/;   
	            return reg.test(value);   
	        },   
	        message: '邮编必须是非0开始的6位数字.'  
	    },   
	    //用户账号验证(只能包括 _ 数字 字母)    
	    account: {//param的值为[]中值   
	        validator: function (value, param) {   
	            if (value.length < param[0] || value.length > param[1]) {   
	                $.fn.validatebox.defaults.rules.account.message = '用户名长度必须在' + param[0] + '至' + param[1] + '范围';   
	                return false;   
	            } else {   
	                if (!/^[\w]+$/.test(value)) {   
	                    $.fn.validatebox.defaults.rules.account.message = '用户名只能数字、字母、下划线组成.';   
	                    return false;   
	                } else {   
	                    return true;   
	                }   
	            }   
	        }, message: ''  
	    }   
	})
	

	
	
		
</script>	
</head>
<body class="easyui-layout" >
	<div region="center">
	
		<table id="grid">
		
		</table>
	</div>
	
	<div class="easyui-window" title="员工详情" id="addStandardWindow" collapsible="false" minimizable="false" maximizable="false" style="top:50px;left:200px">
		<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
			<div class="datagrid-toolbar">
				<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >保存</a>
			</div>
		</div>
		<div region="center" style="overflow:auto;padding:5px;" border="false">
			<form id="sform">
				<table class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="4">员工详情</td>
					</tr>
					
					<tr>
						<td>部门</td>
						<td>
						<input class="easyui-combobox"
									required="true"
									name="sDeptId"
									id="dept"
								></input></td>
					
						<td>姓名</td>
						<td>
						<input name="sName" type="text" class="easyui-textbox" required="true" /></td>
					</tr>
					<input name="sId" type="hidden" class="easyui-textbox"/>
					<tr>
						<td>登录名</td>
						<td><input name="sLoginName" type="text" class="easyui-textbox" required="true" missingMessage="不能为空" validtype="account(4,20)" /></td>
						
					
						<td>邮箱</td>
						<td><input name="sEmail" type="text" class="easyui-textbox" validtype="email" required="true"/></td>
					</tr>
					
					<tr>
						<td>电话</td>
						<td><input name="sPhone" type="text" class="easyui-textbox" validtype="mobile" required="true"  /></td>
					
						<td>密码</td>
						<td><input name="sPwd" id="sPwd" type="password" class="easyui-textbox"  required="true"  /></td>

					</tr>

	
					<tr>
						<td>职务</td>
						<td><input name="sTitle" type="text" class="easyui-textbox" required="true"  /></td>
						
					
						<td>分机号</td>
						<td><input name="sSubPhone" type="text" class="easyui-textbox"  required="true" /></td>
					</tr>
					<tr>
						<td>QQ号</td>
						<td><input name="sQQ" type="text" class="easyui-textbox" required="true"  /></td>
						
					
						<td>微信号</td>
						<td><input name="sWeChat" type="text" class="easyui-textbox" required="true"  /></td>
					</tr>
					<tr>
						<!-- <td>用户类型</td>
						<td>
						<select  name="userType" id="userType" class="easyui-combobox easyui-validatebox"  required="true">
						  <option value="0">普通员工</option>
						  <option value="1">管理员</option>
						</select>
						</td> -->

						<td>是否在职<input name="userType" type="hidden" value="1"   /></td>
						<td>
						<select  name="sStatus" id="sStatus" class="easyui-combobox easyui-validatebox"  required="true">
						  <option value="0">在职</option>
						  <option value="1">离职</option>
						</select>
						</td>
					</tr>
					<tr id="tr_role_warning">
						<td colspan="4">*<a href="javascript:void(0)" onclick="showRoleElement()" >重置用户权限</a>此操作将清除用户原来权限，请谨慎操作.</td>
 					</tr>
					<tr id="tr_role">
						<td id="td_role_tag">添加角色权限</td >
						<td id="td_role_input">
						<input class="easyui-combobox"   editable="false",
 									name="menuIds"
									id="menuIds"
									data-options="
											url:'${pageContext.request.contextPath}/page.roleListAll.ashx',
											method:'get',
											valueField:'menuIds',
											textField:'sLoginName',
											panelHeight:'300'
						"></td >
						<td ></td ><td ></td >
 					</tr>
 					 
 				<!-- 	<tr>
						<td colspan="4">*在团装模块中，管理员能看到所有数据，普通用户只能看到自己上传的数据.</td>
 					</tr> -->
 					<tr>
						<td colspan="4">*设置为离职后，将无法登陆系统.</td>
 					</tr>
					</table>
			</form>
		</div>
	</div>
	
		<div id="tb" style="padding:5px;height:auto">
		<div style="margin-bottom:5px">
			<a href="#" id="add" class="easyui-linkbutton" iconCls="icon-add" plain="true" >增加</a>
			<a href="#" id="edit" class="easyui-linkbutton" iconCls="icon-edit" plain="true">编辑</a>
			<!-- <a href="#" id="add_role" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加角色权限</a> -->
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;姓名: <input id="searchcontent" class="easyui-textbox" style="width:80px">
			<a href="#"  id="search" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
			<!-- <a href="#"  id="export" class="easyui-linkbutton" iconCls="icon-redo">导出</a> -->
		
		</div>
	</div>
</body>
</html>